import 'package:hkzf/utils/string_is_null_or_empty.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';

enum StoreKeys { token, userInfo }

class Store {
  static StoreKeys storeKeys;
  final SharedPreferences _store;

  // 获取缓存，并返回一个实例化对象
  static Future<Store> getInstance() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return Store._internal(prefs);
  }

  Store._internal(this._store);

  getString(StoreKeys key) async {
    return _store.get(key.toString());
  }

  setString(StoreKeys key, String value) async {
    return _store.setString(key.toString(), value);
  }

  getMap(StoreKeys key) async {
    var tar = _store.get(key.toString());
    if (!stringIsNullOrEmpty(tar)) {
      return json.decode(tar);
    } else {
      return new Map<String, String>();
    }
  }

  setMap(StoreKeys key, Map value) async {
    return _store.setString(key.toString(), json.encode(value));
  }

  getStringList(StoreKeys key) async {
    return _store.getStringList(key.toString());
  }

  setStringList(StoreKeys key, List<String> value) async {
    return _store.setStringList(key.toString(), value);
  }
}
